﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Brain.Bepu;
using BEPUphysics;
using Microsoft.Xna.Framework;

namespace Brain.FPS
{
    public class GamerController : IGamerController
    {
        public Vector3 PositionOnScreen;

        /// <summary>
        /// The name of this Gamer Controller
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// The Gamer Information, like Player Index and Gamer Tag
        /// </summary>
        public PlayerInfo PlayerInfo { get; set; }

        /// <summary>
        /// The Character this Controller is using
        /// </summary>
        public CharacterInfo CharInfo { get; set; }

        /// <summary>
        /// If the Gamer is Scoping
        /// </summary>
        public bool IsScoping { get; set; }
        /// <summary>
        /// If the Gamer is Running
        /// </summary>
        public bool IsRunning { get; set; }
        /// <summary>
        /// If the Gamer is Walking
        /// </summary>
        public bool IsWalking { get; set; }
        /// <summary>
        /// If the Gamer is Crouching
        /// </summary>
        public bool IsCrouching { get; set; }

        /// <summary>
        /// The Current Camera
        /// </summary>
        public IGamerCamera Camera { get; set; }
        public IGamerWeaponManager Weapon { get; set; }
        public IGamerControllerInput Input { get; set; }

        public bool ShouldDraw { get; set; }
        public bool ShouldUpdate { get; set; }

        public WorldObject WorldObject { get; set; }

        public GamerController(PlayerInfo info, CharacterInfo character, float height, float radius)
        {
            this.PlayerInfo = info;
            this.CharInfo = character;
            this.Name = NameUtil.GetUniqueName("GamerController");

            if (!string.IsNullOrEmpty(this.CharInfo.ModelPlace))
            {
                this.WorldObject = new WorldObject(this.CharInfo.ModelPlace);
            }
            else
            {
                this.WorldObject = new WorldObject();
            }

            Camera = new DefaultGamerCamera(this);
            Weapon = new DefaultGamerWeaponManager(this);
            Input = new DefaultGamerControllerInput(this, height, radius);

            ShouldDraw = true;
            ShouldUpdate = true;

            Engine.Instance.CameraManager.Camera3D = Camera;
        }
        public GamerController(PlayerInfo info, CharacterInfo character, float height, float radius, 
            IGamerCamera camera, IGamerWeaponManager weapon, IGamerControllerInput input, bool makeIfNull)
        {
            this.PlayerInfo = info;
            this.CharInfo = character;
            this.Name = NameUtil.GetUniqueName("GamerController");

            if (!string.IsNullOrEmpty(this.CharInfo.ModelPlace))
            {
                this.WorldObject = new WorldObject(this.CharInfo.ModelPlace);
            }
            else
            {
                this.WorldObject = new WorldObject();
            }

            if (camera == null && makeIfNull)
            {
                Camera = new DefaultGamerCamera(this);
            }
            else
            {
                Camera = camera;
            }
            if (weapon == null && makeIfNull)
            {
                Weapon = new DefaultGamerWeaponManager(this);
            }
            else
            {
                Weapon = weapon;
            }
            if (input == null && makeIfNull)
            {
                Input = new DefaultGamerControllerInput(this, height, radius);
            }
            else
            {
                Input = input;
            }

            ShouldDraw = true;
            ShouldUpdate = true;

            Engine.Instance.CameraManager.Camera3D = Camera;
        }

        public virtual void Update()
        {
            Weapon.Update();

            WorldObject.Position = this.Camera.Position;
            WorldObject.Update();
        }

        public virtual void Draw()
        {
            Input.Update();
            Camera.Draw();
            Weapon.Draw();
            WorldObject.Draw();
        }

        public bool ShouldSerialize { get; set; }

        public void SerializeData(SerializationData parentData, int index)
        {
            throw new NotImplementedException();
        }

        public void DeserializeData(SerializationData parentData, int index)
        {
            throw new NotImplementedException();
        }

        
    }
}
